/**
 * 
 */
package com.huawei.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.huawei.struts.form.ProgramForm;
import com.huawei.struts.service.IProgramService;
import com.huawei.struts.util.PageBean;
import com.st.model.LjwProgramT;

/**
 * @author huganpu
 *
 */
public class ProgramAction extends BaseAction {

	private static Logger logger=Logger.getLogger(ProgramAction.class);
	
	private final String returnUrl="/program.do?method=init";
	
	private IProgramService programService;
	
	public void setProgramService(IProgramService programService) {
		this.programService = programService;
	}

	/**
	 * 初始化用户列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProgramForm ProgramForm =(ProgramForm)form;
		logger.debug("into Program init method ...");
		ProgramForm.setProgram(new LjwProgramT());//清空所有条件
		PageBean pageBean=programService.getProgramList(ProgramForm.getProgram(), ProgramForm.getPageBean());
		request.setAttribute("pageBean", pageBean);
		return mapping.findForward("toList");
	}
	
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProgramForm ProgramForm =(ProgramForm)form;
		logger.debug("into Program search method ...");
		PageBean pageBean=programService.getProgramList(ProgramForm.getProgram(), ProgramForm.getPageBean());
		request.setAttribute("pageBean", pageBean);
		return mapping.findForward("toList");
	}
	
	/**
	 * 跳转到用户创建页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toCreateProgram(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProgramForm ProgramForm =(ProgramForm)form;
		logger.debug("into Program toCreateProgram method ...");
		this.saveToken(request);
		return mapping.findForward("toAdd");
	}
	
	/**
	 * 创建用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward createProgram(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProgramForm ProgramForm =(ProgramForm)form;
		logger.debug("into Program createProgram method ...");
		String village=request.getParameter("village");
		String channel=request.getParameter("channel");
		String villageName=request.getParameter("villageName");
		String channelName=request.getParameter("channelName"); 
		if(this.isTokenValid(request,true))
		{
			programService.addProgram(ProgramForm.getProgram(),village,villageName,channel,channelName);
			return super.goSuccess("操作成功", returnUrl, request, response, mapping);
		}
		else
		{
			return super.goFail("表单不能重复提交", returnUrl, request, response,mapping);
		}
	}
	
	/**
	 * 跳转到用户修改页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdateProgram(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProgramForm ProgramForm =(ProgramForm)form;
		logger.debug("into Program toUpdateProgram method ...");
		Integer id=new Integer(request.getParameter("id"));
		LjwProgramT program=programService.getProgramInfoByid(id);
		ProgramForm.setProgram(program);
		request.setAttribute("program", program);
		this.saveToken(request);
		return mapping.findForward("toUpdate");
	}
	
	/**
	 * 修改用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateProgram(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProgramForm ProgramForm =(ProgramForm)form;
		logger.debug("into Program updateProgram method ...");
		String village=request.getParameter("village");
		String channel=request.getParameter("channel");
		String villageName=request.getParameter("villageName");
		String channelName=request.getParameter("channelName"); 
		if(this.isTokenValid(request,true))
		{
			programService.modifyProgram(ProgramForm.getProgram(), village, villageName, channel, channelName);
			return super.goSuccess("操作成功", returnUrl, request, response, mapping);
		}
		else
		{
			return super.goFail("表单不能重复提交", returnUrl, request, response,mapping);
		}
	}
	
	public ActionForward delProgram(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProgramForm ProgramForm =(ProgramForm)form;
		Integer id=new Integer(request.getParameter("id"));
		ProgramForm.getProgram().setProgramId(id);
		programService.delProgram(ProgramForm.getProgram());
		return super.goSuccess("操作成功", returnUrl, request, response, mapping); 
	}

}
